// David Ciani <dciani@ucsc.edu>
// $Id: Hand.java 4 2011-03-08 11:27:57Z dciani@davidciani.com $

import java.util.*;

class Hand {

    private Deck deck;

    private ArrayList<Card> hand;

    public Hand(Deck deck) {
        this.deck = deck;
        this.hand = new ArrayList<Card>(5);
        
        // draw the first two cards
        this.hand.add(deck.draw());
        this.hand.add(deck.draw());

    }

    public String toString() {
        String out = new String();
        for(Card card: hand) {
            out += card + "\n";
        }
        return out;
    }

    public void hit() throws HandFullException, HandOverException {
        if(this.value() < 21) {
            if(this.hand.size() == 5) throw new HandFullException();
            this.hand.add(deck.draw());
        }
        else throw new HandOverException();
    }

    public int value() {
        int value = 0;
        boolean seenace = false;
        for(Card card : hand) {
            
            int cardvalue = card.getValue();

            if(seenace && card.isAce()) cardvalue = 1;
            value += cardvalue;

            if(card.isAce()) seenace = true;
        }
        return value;
    }
}

class HandFullException extends Exception {
    public HandFullException() {
        super();
    }
}

class HandOverException extends Exception {
    public HandOverException() {
        super();
    }
}
